package com.xja.house.ssm.controller;

import com.xja.house.ssm.entity.House;
import com.xja.house.ssm.entity.Users;
import com.xja.house.ssm.service.HouseService;
import com.xja.house.ssm.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("houtai")
public class HouTaiController {
    @Autowired
    private UsersService usersService;
    @Autowired
    private HouseService houseService;
    @RequestMapping("fabu")
    public String fabu(House house, HttpSession session){
        try{
            Users loginuser = (Users)session.getAttribute("loginuser");
            System.out.println(loginuser.getName());
            if(loginuser==null){
                return "redirect:/page/login.jsp";
            }
            house.setUserId(loginuser.getId());
            houseService.addHouse(house);
            return "redirect:/houtai/guanli.do";
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return "redirect:/error.jsp";
    }

    @RequestMapping("guanli")
    public String guanli(Model model){
        try{
            List<House> houseList = houseService.guanliSelectAll();
            model.addAttribute("houseList",houseList);
            return "guanli";
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return "redirect:/error.jsp";
    }

    @RequestMapping("zhuce")
    public String zhuce(Users users){
        try{
            users.setIsadmin("false");
            usersService.addUsers(users);
            return "redirect:/page/login.jsp";
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return "redirect:/error.jsp";
    }

    @RequestMapping("fenzhi1")
    public String fenzhi1(Users users){
        try{

            return "redirect:/page/fenzhiyi.jsp";
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return "redirect:/error.jsp";
    }

}
